package event.dao;

import java.util.HashMap;
import java.util.List;


import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import event.model.EventModel;

public class EventDao {
	private MongoTemplate mongoTemplate;
	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}
	
	
	public List<EventModel> selectEvent(HashMap param){
		Criteria criteria = new Criteria();
		Query query = new Query(criteria);
		System.out.println("query " + query.getQueryObject().toString());
		//criteria.and(key)
		return mongoTemplate.find(query, EventModel.class);
	}
	
	public int insertEvent(EventModel param){
		
		mongoTemplate.save(param);
		System.out.println("param " + param.toString());
		return 1;
	}
	
	public int updateEvent(HashMap param){
		Criteria criteria = new Criteria();
		criteria.where("_id").is(param.get("albumId"));
		Query query = new Query(criteria);
		System.out.println("query " + query.getQueryObject().toString());
		
		Update update = new Update();
		update.set("albumName", param.get("albumName"));
		
		mongoTemplate.findAndModify(query, update, HashMap.class , "Album");
		return 1;
	}
	
	public int deleteEvent(HashMap param) {
		System.out.println("deleteEvent Dao" + param.get("albumId").toString());
		Criteria criteria = new Criteria();
		criteria.where("_id").is(param.get("albumId"));
		Query query = new Query(criteria);
		
		System.out.println("query " + query.getQueryObject().toString());
		mongoTemplate.remove(query, "Album");
		return 1;
	}
}
